<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        .wrap {
            width: 600px;
            margin: 0 auto;
            margin-top: 50px;
        }

        .box {
            line-height: 45px;
            display: flex;
        }

        .box li {
            flex: 1;
            text-align: center;
            border: 1px solid #000;
            float: left;
        }

        .box .active {
            background-color: #f00;
        }

        .content>div {
            height: 400px;
            border: 1px solid #000;
            text-align: center;
            line-height: 400px;
            display: none;
        }

        .content .active {
            display: block;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <ul id="box" class="box">
            <li class="active">01</li>
            <li>02</li>
            <li>03</li>
            <li>04</li>
        </ul>
        <div class="content">
            <div class="active">01</div>
            <div>02</div>
            <div>03</div>
            <div>04</div>
        </div>
    </div>
</body>
<script>


    var box = document.getElementsByClassName("box")[0];
    var liList = box.getElementsByTagName("li");

    var content = document.getElementsByClassName("content")[0];
    var tabList = content.children;

    // for (var i = 0; i < liList.length; i++) {
    //     var li = liList[i];
    //     (function (li, i) {
    //         li.onclick = function () {
    //             console.log(i, li);
    //         }
    //     })(li, i);
    // }
    // console.log(i, li);

    // var a = 100;
    // function test() {
    //     var a = 10;
    //     console.log(a);
    // }

    // let  
    // {
    //     let a = 10;
    // }

    var index = 0;

    for (let i = 0; i < liList.length; i++) {
        let li = liList[i];
        li.onclick = function () {
            // console.log(i, li);
            clearInterval(timer);
            index = i;
            for (var j = 0; j < liList.length; j++) {
                liList[j].className = "";
                tabList[j].className = "";
            }

            liList[i].className = "active";
            tabList[i].className = "active";
        }
    }


    var timer = setInterval(function () {
        index++;
        if (index == 4) {
            index = 0;
        }
        // console.log(index);
        for (var j = 0; j < liList.length; j++) {
            liList[j].className = "";
            tabList[j].className = "";
        }

        liList[index].className = "active";
        tabList[index].className = "active";

    }, 1000)






</script>

</html>